{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import lightgbm as lgb\n",
    "from datetime import datetime\n",
    "from sklearn.metrics import roc_auc_score\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from sklearn.model_selection import StratifiedKFold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_path = '../../../contest/train/'\n",
    "stage_path = '../../../contest/B榜/'\n",
    "stage = 'B'\n",
    "end_date_train = datetime(1993,11,29)\n",
    "end_date_test = datetime(1994,1,30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_train = pd.read_csv('../../../contest/train/DZ_TARGET_TRAIN.csv')\n",
    "df_test = pd.read_csv('../../../contest/B榜/DZ_TARGET_TESTB.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_train_user = pd.DataFrame({'CUST_NO':df_train.CUST_NO})\n",
    "df_test_user = pd.DataFrame({'CUST_NO':df_test.cust_no})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 掌银行为表（DZ_MBNK_BEHAVIOR）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp_train = pd.read_csv(os.path.join(train_path,'DZ_MBNK_BEHAVIOR.csv'))\n",
    "tmp_test = pd.read_csv(os.path.join(stage_path,f'DZ_MBNK_BEHAVIOR_{stage}.csv'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fe_behavior(df):\n",
    "    df = df.drop('UserId',axis=1)\n",
    "    df = df.rename({'pid':'CUST_NO'},axis=1)\n",
    "    df_grp = df.groupby('CUST_NO').agg(\n",
    "        device_nunique = ('DeviceId','nunique'),\n",
    "        page_nunique = ('OperationPage','nunique'),\n",
    "        date_nunique = ('addfielddate','nunique'),\n",
    "        behavior_cnt = ('DeviceId','count')\n",
    "    )\n",
    "    df_grp = df_grp.reset_index()\n",
    "    return df_grp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp_train = fe_behavior(tmp_train)\n",
    "tmp_test = fe_behavior(tmp_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CUST_NO</th>\n",
       "      <th>device_nunique</th>\n",
       "      <th>page_nunique</th>\n",
       "      <th>date_nunique</th>\n",
       "      <th>behavior_cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0001a72742eac77fcd8b225e27e382b7</td>\n",
       "      <td>1</td>\n",
       "      <td>19</td>\n",
       "      <td>3</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0005266bdad8fd06074ea1ebea668879</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>7</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>00078bace2ce51843924311f088a6071</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000e7f999b3dd7162e8b0a16a33d620d</td>\n",
       "      <td>1</td>\n",
       "      <td>14</td>\n",
       "      <td>4</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>00102c9a9b27cfa57351bb18a7aa7d2d</td>\n",
       "      <td>3</td>\n",
       "      <td>35</td>\n",
       "      <td>6</td>\n",
       "      <td>121</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                            CUST_NO  device_nunique  page_nunique  \\\n",
       "0  0001a72742eac77fcd8b225e27e382b7               1            19   \n",
       "1  0005266bdad8fd06074ea1ebea668879               1             7   \n",
       "2  00078bace2ce51843924311f088a6071               1             1   \n",
       "3  000e7f999b3dd7162e8b0a16a33d620d               1            14   \n",
       "4  00102c9a9b27cfa57351bb18a7aa7d2d               3            35   \n",
       "\n",
       "   date_nunique  behavior_cnt  \n",
       "0             3            79  \n",
       "1             7            25  \n",
       "2             1             1  \n",
       "3             4            42  \n",
       "4             6           121  "
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tmp_train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp_train = df_train_user.merge(tmp_train,on='CUST_NO',how='left')\n",
    "tmp_test = df_test_user.merge(tmp_test,on='CUST_NO',how='left')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 最后登录日期和时间间隔统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp_train1 = pd.read_csv(os.path.join(train_path,'DZ_MBNK_BEHAVIOR.csv'))\n",
    "tmp_test1 = pd.read_csv(os.path.join(stage_path,f'DZ_MBNK_BEHAVIOR_{stage}.csv'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>DeviceId</th>\n",
       "      <th>UserId</th>\n",
       "      <th>OperationPage</th>\n",
       "      <th>addfielddate</th>\n",
       "      <th>pid</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>9f525a56b0f80267de24d41b323b2452</td>\n",
       "      <td>4be58df9accea40c0ad26177081d380f</td>\n",
       "      <td>b0c296cf24f7a4a6fffb705cc07a6b1a</td>\n",
       "      <td>19931031</td>\n",
       "      <td>5ff5ba1502a92fe7d74e12a4220afc5e</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2583347767b09dac3d06f95b6a53589c</td>\n",
       "      <td>c3be0cf79d0867f56c2c23bb9251bbab</td>\n",
       "      <td>f5f70124dace754e9e28aa2ac78950a5</td>\n",
       "      <td>19931109</td>\n",
       "      <td>d252cf1128986324c6b8f3c8dc801ec1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2583347767b09dac3d06f95b6a53589c</td>\n",
       "      <td>d1b21668b91a40f7eef78c894a3ee4fc</td>\n",
       "      <td>81dc5d2db7642c4bc458367fa6da5ff1</td>\n",
       "      <td>19931103</td>\n",
       "      <td>fc8e82609568091b682f95f3767d413d</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2583347767b09dac3d06f95b6a53589c</td>\n",
       "      <td>8978d9b1a76e82f5b6890da4577446c3</td>\n",
       "      <td>55d096303612713cb957c8d266aa3825</td>\n",
       "      <td>19931120</td>\n",
       "      <td>25416a9a32bc8ce7a51242fd9ed64c40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5e8a19e6cb9dd79e93439534a60a5ca2</td>\n",
       "      <td>f22571150aa3d07def6773613b54a909</td>\n",
       "      <td>6234008d382aa5aeac4d033686cb2cca</td>\n",
       "      <td>19931005</td>\n",
       "      <td>93b798f50aa76ffffb1bf5ca7b76523f</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           DeviceId                            UserId  \\\n",
       "0  9f525a56b0f80267de24d41b323b2452  4be58df9accea40c0ad26177081d380f   \n",
       "1  2583347767b09dac3d06f95b6a53589c  c3be0cf79d0867f56c2c23bb9251bbab   \n",
       "2  2583347767b09dac3d06f95b6a53589c  d1b21668b91a40f7eef78c894a3ee4fc   \n",
       "3  2583347767b09dac3d06f95b6a53589c  8978d9b1a76e82f5b6890da4577446c3   \n",
       "4  5e8a19e6cb9dd79e93439534a60a5ca2  f22571150aa3d07def6773613b54a909   \n",
       "\n",
       "                      OperationPage  addfielddate  \\\n",
       "0  b0c296cf24f7a4a6fffb705cc07a6b1a      19931031   \n",
       "1  f5f70124dace754e9e28aa2ac78950a5      19931109   \n",
       "2  81dc5d2db7642c4bc458367fa6da5ff1      19931103   \n",
       "3  55d096303612713cb957c8d266aa3825      19931120   \n",
       "4  6234008d382aa5aeac4d033686cb2cca      19931005   \n",
       "\n",
       "                                pid  \n",
       "0  5ff5ba1502a92fe7d74e12a4220afc5e  \n",
       "1  d252cf1128986324c6b8f3c8dc801ec1  \n",
       "2  fc8e82609568091b682f95f3767d413d  \n",
       "3  25416a9a32bc8ce7a51242fd9ed64c40  \n",
       "4  93b798f50aa76ffffb1bf5ca7b76523f  "
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tmp_train1.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "def date_diff(df):\n",
    "    df['date_new'] = pd.to_datetime(df['addfielddate'],format = '%Y%m%d')\n",
    "    df2 = df.drop_duplicates(subset=['addfielddate','pid'],keep='first')\n",
    "    df2['date_diff'] = df2.sort_values('addfielddate').groupby('pid')['date_new'].diff().dt.days\n",
    "    df2 = df2.sort_values('addfielddate').dropna()\n",
    "    return df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-33-cf3911982dd5>:4: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df2['date_diff'] = df2.sort_values('addfielddate').groupby('pid')['date_new'].diff().dt.days\n"
     ]
    }
   ],
   "source": [
    "tmp_train2 = date_diff(tmp_train1)\n",
    "tmp_test2 = date_diff(tmp_test1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "#tmp_train2[tmp_train2['pid']=='5ff5ba1502a92fe7d74e12a4220afc5e']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fe_behavior_date(df,enddate):\n",
    "    df = df.drop('UserId',axis=1)\n",
    "    df = df.rename({'pid':'CUST_NO'},axis=1)\n",
    "    df_grp = df.groupby('CUST_NO').agg(\n",
    "        behavior_last_date = ('addfielddate','max'),  #最后登录日期\n",
    "      #  behavior_interval_max = ('date_diff','max'),  #最大时间间隔\n",
    "      #  behavior_interval_min = ('date_diff','min'),\n",
    "     #   behavior_interval_mean = ('date_diff','mean'),\n",
    "     #   behavior_interval_std = ('date_diff','std'),\n",
    "     #   behavior_interval_count = ('date_diff','count')\n",
    "    )\n",
    "  #  df_grp['behavior_interval_count'] = df_grp['behavior_interval_count'] + 1\n",
    "    df_grp['behavior_last_date'] = (enddate - pd.to_datetime(df_grp['behavior_last_date'],format = '%Y%m%d')).dt.days\n",
    "    df_grp = df_grp.reset_index()\n",
    "    return df_grp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp_train3 = fe_behavior_date(tmp_train2,end_date_train)\n",
    "tmp_test3 = fe_behavior_date(tmp_test2,end_date_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "#tmp_train3[tmp_train3['CUST_NO']=='5ff5ba1502a92fe7d74e12a4220afc5e']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp_train = tmp_train.merge(tmp_train3,on='CUST_NO',how='left')\n",
    "tmp_test = tmp_test.merge(tmp_test3,on='CUST_NO',how='left')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp_train['behavior_last_date'] = tmp_train['behavior_last_date'].fillna(999)\n",
    "tmp_test['behavior_last_date'] = tmp_test['behavior_last_date'].fillna(999)\n",
    "tmp_train = tmp_train.fillna(0)\n",
    "tmp_test = tmp_test.fillna(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CUST_NO</th>\n",
       "      <th>device_nunique</th>\n",
       "      <th>page_nunique</th>\n",
       "      <th>date_nunique</th>\n",
       "      <th>behavior_cnt</th>\n",
       "      <th>behavior_last_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>235e4e193124d8c55095cf3f0f0d8f35</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>999.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>f1b5ca32a8f7ef5430f5775c00ff3f60</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>999.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>51be6f380b408edeb7779b76e016dcd3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>999.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ccd7e33ccbe7e9dd4246a2959f666c0a</td>\n",
       "      <td>2.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>101.0</td>\n",
       "      <td>29.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>069f48f51bf6be5bcbdc9af52bb20970</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>999.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>e522f6795b1f05afeaed7a7558f11d76</td>\n",
       "      <td>1.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>15.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>d1812e2668acc6f42855f3e7df6f9d0d</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>999.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>6ca8ee5cd27170e0e6f3bdedaa794502</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>999.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>48dedec59cbceff7e61a01412b9d887e</td>\n",
       "      <td>1.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>999.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>76db3f25953155cb3806741d8b828439</td>\n",
       "      <td>2.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>89.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>86defa7300967b6ab8b1deea6909b354</td>\n",
       "      <td>1.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>82.0</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>5299b6dffdcc48a6e247841245513cec</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>999.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>3c9a77e0cd3bc0966bb7861729542e3b</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>999.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>00a7751aa5aab6c5f49b8d5ae97909d1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>999.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>f9bfb27d9a24e8e74385b18b80a38ae2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>54.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>5c9a28a0adbfee63c80e9a6145af5edb</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>999.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>1ebd81eed037200a2b32ca3759c3768e</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>999.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>0ddd0649107dc8c3977a577eaef6bf87</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>999.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>e7ae205bc597e7c15186863dcdeab470</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>999.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>0e24f13330d484361e293f5de8f814c7</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>999.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             CUST_NO  device_nunique  page_nunique  \\\n",
       "0   235e4e193124d8c55095cf3f0f0d8f35             0.0           0.0   \n",
       "1   f1b5ca32a8f7ef5430f5775c00ff3f60             0.0           0.0   \n",
       "2   51be6f380b408edeb7779b76e016dcd3             0.0           0.0   \n",
       "3   ccd7e33ccbe7e9dd4246a2959f666c0a             2.0          21.0   \n",
       "4   069f48f51bf6be5bcbdc9af52bb20970             0.0           0.0   \n",
       "5   e522f6795b1f05afeaed7a7558f11d76             1.0          13.0   \n",
       "6   d1812e2668acc6f42855f3e7df6f9d0d             0.0           0.0   \n",
       "7   6ca8ee5cd27170e0e6f3bdedaa794502             0.0           0.0   \n",
       "8   48dedec59cbceff7e61a01412b9d887e             1.0          10.0   \n",
       "9   76db3f25953155cb3806741d8b828439             2.0          14.0   \n",
       "10  86defa7300967b6ab8b1deea6909b354             1.0          19.0   \n",
       "11  5299b6dffdcc48a6e247841245513cec             0.0           0.0   \n",
       "12  3c9a77e0cd3bc0966bb7861729542e3b             0.0           0.0   \n",
       "13  00a7751aa5aab6c5f49b8d5ae97909d1             1.0           1.0   \n",
       "14  f9bfb27d9a24e8e74385b18b80a38ae2             1.0          11.0   \n",
       "15  5c9a28a0adbfee63c80e9a6145af5edb             0.0           0.0   \n",
       "16  1ebd81eed037200a2b32ca3759c3768e             0.0           0.0   \n",
       "17  0ddd0649107dc8c3977a577eaef6bf87             0.0           0.0   \n",
       "18  e7ae205bc597e7c15186863dcdeab470             0.0           0.0   \n",
       "19  0e24f13330d484361e293f5de8f814c7             0.0           0.0   \n",
       "\n",
       "    date_nunique  behavior_cnt  behavior_last_date  \n",
       "0            0.0           0.0               999.0  \n",
       "1            0.0           0.0               999.0  \n",
       "2            0.0           0.0               999.0  \n",
       "3            5.0         101.0                29.0  \n",
       "4            0.0           0.0               999.0  \n",
       "5            2.0          40.0                15.0  \n",
       "6            0.0           0.0               999.0  \n",
       "7            0.0           0.0               999.0  \n",
       "8            1.0          26.0               999.0  \n",
       "9           12.0          89.0                 0.0  \n",
       "10           3.0          82.0                 7.0  \n",
       "11           0.0           0.0               999.0  \n",
       "12           0.0           0.0               999.0  \n",
       "13           1.0           6.0               999.0  \n",
       "14          14.0          54.0                 3.0  \n",
       "15           0.0           0.0               999.0  \n",
       "16           0.0           0.0               999.0  \n",
       "17           0.0           0.0               999.0  \n",
       "18           0.0           0.0               999.0  \n",
       "19           0.0           0.0               999.0  "
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tmp_train.head(20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(50162, 6) (5024, 6)\n"
     ]
    }
   ],
   "source": [
    "print(tmp_train.shape,tmp_test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp_train.to_csv('../fea/train_tr_behavior.csv',index=False)\n",
    "tmp_test.to_csv('../fea/test_tr_behavior.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
